<?php

namespace App\Services\spider;

use App\Model\CommonSpiderItem;
use App\Services\UtilService;
use Illuminate\Support\Facades\DB;

class SpiderItemRepo implements IDataRepo
{


    public function getCount($taskNo)
    {
        DB::select("
       SELECT
       (SELECT count(1) n FROM common_spider_items WHERE task_no = '$taskNo') webCount,
       (SELECT count(1) n FROM common_spider_items WHERE task_no = '$taskNo' AND is_target = 1 ) targetCount
       ");
    }

    public function filterExists($taskNo, array $urls): array
    {
        $unDupUrls = [];
        foreach ($urls as $url) {
            $unDupUrls[$url] = 1;
        }
        $unDupUrls=array_keys($unDupUrls);
        $arr = [];
        $items = CommonSpiderItem::query()
            ->where("task_no", '=', $taskNo)
            ->whereIn("url", $unDupUrls)
            ->get("url");
        foreach ($unDupUrls as $url) {
            if ($items->where("url", '=', $url)->count() < 1) {
                $arr[] = $url;
            }
        }
        return $arr;
    }


    public function getHtmlItem($taskNo, $url)
    {

        return CommonSpiderItem::query()
            ->where("task_no", '=', $taskNo)
            ->where("html_code", '=', CommonSpiderItem::html_code_success)
            ->where("url", '=', $url)
            ->first();

    }



    public function errorItems($taskNo, $limit = 50)
    {
        return CommonSpiderItem::query()
            ->where("task_no", '=', $taskNo)
            ->where("html_code", '=', CommonSpiderItem::html_code_handle_error)
            ->orderBy("url")
            ->limit($limit)
            ->get(['url']);
    }

    public function unHandleItems($taskNo, $limit = 50)
    {
        $list=CommonSpiderItem::query()
            ->where("task_no", '=', $taskNo)
            ->where("html_code", '=', CommonSpiderItem::html_code_init)
            ->whereRaw("(
            url like 'https://shuju.qgyyzs.net/qgyydq/details%'
            OR url like 'https://shuju.qgyyzs.net/qgyydq/?key%'   
            OR is_target=1
            )")
            ->limit($limit)
            ->get(["url"]);
        if($list->count()>0){
            return $list;
        }
        return CommonSpiderItem::query()
            ->where("task_no", '=', $taskNo)
            ->where("html_code", '=', CommonSpiderItem::html_code_init)
            ->orderBy("url")
            ->limit($limit)
            ->get(['url']);
    }
    public function handledItem($taskNo, $url): bool
    {
        return CommonSpiderItem::query()
            ->where("url", '=', $url)
            ->where("task_no", '=', $taskNo)
            ->whereIn("html_code", CommonSpiderItem::handledCode())
            ->exists();
    }

    public function updateItem(string $taskNo, string $url, array $arr): bool
    {
      $first=  CommonSpiderItem::query()
            ->where("url", '=', $url)
            ->where("task_no", '=', $taskNo)
            ->first();
      if($first){
          foreach ($arr as $key=>$value){
              $first->$key=$value;
          }
          return $first->save();
      }

      return false;


    }


}